<?php
 
  
function getMilestones($con,$db_name,$requestVars,$sessionEnabled,$type)
{
    $pid = $requestVars['pid'];
    $mid = $requestVars['mid']; // So you can retrieve a specific milestone. 
    $status =  $requestVars['status'];

    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }    

    $query = "SELECT * FROM $db_name.milestones  ";


    if($pid)
    {      
          
      $query = $query. "WHERE  project = $pid";         

    }
    
    if($mid)
    {
        $query = "SELECT * FROM $db_name.milestones where ID = $mid"; // Ignores everything 
    }



    if($status)
    {
        $query = $query." and status = $status";
    }
     $result = mysqli_query($con,$query);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }
    $MilestoneArray = array();
    $counter = 0;
    while($row = mysqli_fetch_assoc($result))
    {

     $id = $row['ID'];     
     $pid = $row['project'];          
     $name = $row['name'];
     $desc = $row['desc'];     
     $start = $row['start'];
     $end = $row['end'];
     $status = $row['status'];



     $Milestone =  new Milestone($id, $pid, $name,$desc, $start, $end,$status);

     $MilestoneArray[$counter] = $Milestone;     

     $counter++;

    }
    if($mid)
    {
        if($type == 'json')
        {            
            return MilestonesResponseJSON(1,"Success!",$id, $pid, $name,$desc, $start, $end,$status);
        }
        else if($type == 'xml')
        {
            return MilestonesResponseXML(1,"Success!",$id, $pid, $name,$desc, $start, $end,$status);
        }       
    }
    else
    {
        if($type == 'json')
        {            
            return ArrayMilestonesResponseJSON(1,"Success!",$MilestoneArray);
        }
        else if($type == 'xml')
        {
            return ArrayMilestonesResponseXML(1,"Success!",$MilestoneArray);
        }    
    }
    
     
}

function addMilestone($con,$db_name,$requestVars,$sessionEnabled,$type)
{
    $pid = $requestVars['pid'];
    $name = $requestVars['name'];
    $desc = $requestVars['desc'];
    $end = $requestVars['end'];
    $status = $requestVars['status'];
    
    if(!$pid)
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(7, "You must pass a project id (pid) ! ");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(7, "You must pass a project id (pid) ! ");
        }       
        
    }
    
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }
    
    $pid = (int) $pid;
    $name = mysqli_real_escape_string($con,$name);
    $desc = mysqli_real_escape_string($con,$desc);
    $start = time();
    $end = strtotime($end);
    $status = (int) $status;

    $ins = mysqli_query($con,"INSERT INTO $db_name.milestones (`project`,`name`,`desc`,`start`,`end`,`status`) VALUES ($pid,'$name','$desc','$start','$end',$status)");
    
    $mid = mysqli_insert_id($con);
    
    $query  = "SELECT * From $db_name.milestones WHERE id = $mid";
    $result = mysqli_query($con,$query);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }

    while($row = mysqli_fetch_assoc($result))
    {
        $mid = $row['ID'];        
        $name = $row['name'];
        $desc = $row['desc'];
        $start = $row['start'];
        $end = $row['end'];        
        $status = $row['status'];
      
    }
    
    
    if ($ins)
    {
        if($type == 'json')
        {            
            return MilestonesResponseJSON(1, "Success!", $mid,$pid,$name,$desc,$start,$end,$status);
        }
        else if($type == 'xml')
        {
            return MilestonesResponseXML(1, "Success!", $mid,$pid,$name,$desc,$start,$end,$status);
        }         
        
    }
    else
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(31,"Failed to create milestone, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(31,"Failed to create milestone, please try again later");
        }       
        
    }        
    
}

    /**
     * Edit a milestone
     *
     * @param int $id Milestone ID
     * @param string $name Name
     * @param string $desc Description
     * @param string $end Day it is due
     * @return bool
     */
function editMilestone($con,$db_name,$requestVars,$sessionEnabled, $type)
{
    
    $mid = $requestVars['mid'];
    $name= $requestVars['name'];
    $desc = $requestVars['desc'];
    $end = $requestVars['end'];
    
    if(!$mid)
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(8, "You must pass a milestone id (mid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(8, "You must pass a milestone id (mid) !");
        }       
         
    }
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }
    // Let's add some smarts, and say the developer doesn't want to always grab and repass what is already there.
    // lets just update what is different. 

    $get ="select * from $db_name.milestones WHERE ID=$mid";
    $result =  mysqli_query($con,$get);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$get]");
    }
    while($row = mysqli_fetch_assoc($result))
    {

        if(!$name)
        {             
            $name = $row['name'];                    
        }

        if(!$desc)
        {
            $desc = $row['name'];
        }

        if(!$end)
        {
            $end = $row['end'];
        } 
        $pid = $row['project'];
        $start = $row['start'];
        $status = $row['status'];
        
        
    }         
    $mid = (int) $mid;
    $name = mysqli_real_escape_string($con,$name);
    $desc = mysqli_real_escape_string($con,$desc);
    $end = strtotime($end); 

    $upd = mysqli_query($con,"UPDATE $db_name.milestones SET `name`='$name', `desc`='$desc', `end`='$end' WHERE ID=$mid");
    if ($upd)
    {   
        if($type == 'json')
        {            
            return MilestonesResponseJSON(1, "Success!", $mid,$pid,$name,$desc,$start,$end,$status);
        }
        else if($type == 'xml')
        {
            return MilestonesResponseXML(1, "Success!", $mid,$pid,$name,$desc,$start,$end,$status);
        }         
    }
    else
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(14,"Failed to update milestone, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(14,"Failed to update milestone, please try again later");
        }       
    }
     
}

/**
 * Delete a milestone
 *
 * @param int $id Milestone ID
 * @return bool
 */
function delMilestone($con, $db_name,$requestVars,$sessionEnabled,$type)
{
    $mid = $requestVars['mid'];
    
    if(!$mid)
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(8, "You must pass a milestone id (mid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(8, "You must pass a milestone id (mid) !");
        }       
         
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } 

    $mid = (int) $mid;

    $nam = mysqli_query($con,"SELECT project,name FROM milestones WHERE ID = $mid");
    $del = mysqli_query($con,"DELETE FROM milestones WHERE ID = $mid");
    $del1 = mysqli_query($con,"DELETE FROM milestones_assigned WHERE milestone = $mid");
    if ($del)
    {
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else
    {
         if($type == 'json')
        {
             return GenericResponseJSON(19, "Failed to delete milestone, please try again later");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(19, "Failed to delete milestone, please try again later!");
        }       
    }         
     
}

function closeMilestone($con, $db_name,$requestVars,$sessionEnabled, $type)
{
    $mid = $requestVars['mid'];
    if(!$mid)
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(8, "You must pass a milestone id (mid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(8, "You must pass a milestone id (mid) !");
        }       
         
    }
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }
    $mid = (int) $mid;

    $upd = mysqli_query($con,"UPDATE $db_name.milestones SET status = 0 WHERE ID = $mid");

    if($requestVars['ignoreTasklist'] != 1) // Done to prevent inf loops when closing tasklist.
    {
        // Close associated Tasklists, (which will also close its appropriate tasks)
        $get ="select * from $db_name.tasklist WHERE milestone=$mid";
        $result =  mysqli_query($con,$get);

        if(!$result)
        {
            die("mysql error: ".mysql_error()."[$get]");
        }
        
        while($row = mysqli_fetch_assoc($result))
        {
            $row['tlid'] = $row['ID'];
            closeTasklist($con,$db_name,$row,$sessionEnabled,$type); //Close associated tasklist.
             
        }
    }

    if ($upd)
    { 
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else
    {
        if($type == 'json')
        {
             return GenericResponseJSON(23, "Failed to close milestone, please try again later");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(23, "Failed to close milestone, please try again later");
        }       
    }      
     
}

function openMilestone($con, $db_name,$requestVars,$sessionEnabled,$type)
{
    $mid = $requestVars['mid'];
        if(!$mid)
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(8, "You must pass a milestone id (mid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(8, "You must pass a milestone id (mid) !");
        }       
         
    }
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','close',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }  
    
    $mid = (int) $mid;

    $upd = mysqli_query($con,"UPDATE $db_name.milestones SET status = 1 WHERE ID = $mid");

    if ($upd)
    {
        
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else
    {
        if($type == 'json')
        {
             return GenericResponseJSON(27, "Failed to open milestone, please try again later");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(27, "Failed to open milestone, please try again later ");
        }       
    }                
}

?>
